package com.example.club.test.recyclerview

import androidx.recyclerview.widget.DiffUtil

class MyDiffCallBack(val oldData:List<MyModel>,val newData:List<MyModel>):DiffUtil.Callback() {
    override fun getOldListSize(): Int {
        return oldData.size
    }

    override fun getNewListSize(): Int {
        return newData.size
    }

    override fun areItemsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldData[oldItemPosition].id ==newData[newItemPosition].id
    }

    override fun areContentsTheSame(oldItemPosition: Int, newItemPosition: Int): Boolean {
        return oldData[oldItemPosition]==newData[newItemPosition]
    }
    /**
     * 局部增量刷新：是同一个对象，但是内容不同
     */
    override fun getChangePayload(oldItemPosition: Int, newItemPosition: Int): Any? {
        val old=oldData[oldItemPosition]
        val new=newData[newItemPosition]
        if (old.isLike!=new.isLike){
            return "like"
        }
        if (old.name != new.name){
            return "name"
        }
        if (old.url != new.url){
            return "url"
        }
        return null
    }
}